package com.xjwlmq.exam20241011;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * @ClassName DictTypeAddServlet
 * @Description TODO:
 * @Author 12994
 * @Date 2024/10/11 20:45
 * @Version 1.0
 **/
@WebServlet("/addDictType")
public class DictTypeAddServlet extends HttpServlet {
    DictTypeDaoImpl dictTypeDao = new DictTypeDaoImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html:charset=UTF-8");

        DictType thisDictType = new DictType();
        thisDictType.setDictName(req.getParameter("dictName"));
        thisDictType.setDictType(req.getParameter("dictType"));
        thisDictType.setStatus(req.getParameter("status"));
        thisDictType.setCreateBy(Integer.parseInt(req.getParameter("createBy")));
        if (!Objects.equals(req.getParameter("updateBy"), "")) {
            thisDictType.setUpdateBy(Integer.parseInt(req.getParameter("updateBy")));
        }
        thisDictType.setRemark(req.getParameter("remark"));

        int count = dictTypeDao.addDictType(thisDictType);
        if (count > 0) {
            resp.getWriter().write(JSON.toJSONString("成功添加了%d行数据.".formatted(count)));
        } else {
            resp.getWriter().write(JSON.toJSONString("没有影响数据..."));
        }
    }
}
